/*     */ package com.zimbra.cs.datasource.imap;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.DataSource;
/*     */ import com.zimbra.cs.datasource.DataSourceFolderMapping;
/*     */ import com.zimbra.cs.datasource.DataSourceManager;
/*     */ import com.zimbra.cs.db.DbDataSource;
/*     */ import com.zimbra.cs.db.DbDataSource.DataSourceItem;
/*     */ import com.zimbra.cs.db.DbImapFolder;
/*     */ import com.zimbra.cs.db.DbImapMessage;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Metadata;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collection;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ImapFolder
/*     */   extends DataSourceFolderMapping
/*     */ {
/*     */   private String localPath;
/*     */   private Long uidValidity;
/*     */   private static final String METADATA_KEY_LOCAL_PATH = "lp";
/*     */   private static final String METADATA_KEY_UID_VALIDITY = "uv";
/*     */   
/*     */   public ImapFolder(DataSource ds, DbDataSource.DataSourceItem dsi)
/*     */     throws ServiceException
/*     */   {
/*  42 */     super(ds, dsi);
/*     */   }
/*     */   
/*     */   public ImapFolder(DataSource ds, int itemId) throws ServiceException {
/*  46 */     super(ds, itemId);
/*     */   }
/*     */   
/*     */   public ImapFolder(DataSource ds, String remoteId) throws ServiceException {
/*  50 */     super(ds, remoteId);
/*     */   }
/*     */   
/*     */   public ImapFolder(DataSource ds, int itemId, String remoteId, String localPath, Long uidValidity) throws ServiceException
/*     */   {
/*  55 */     super(ds, itemId, remoteId);
/*  56 */     setLocalPath(localPath);
/*  57 */     setUidValidity(uidValidity);
/*     */   }
/*     */   
/*  60 */   public String getLocalPath() { return this.localPath; }
/*     */   
/*  62 */   public long getUidValidity() { return this.uidValidity.longValue(); }
/*     */   
/*     */   public void setLocalPath(String localPath) {
/*  65 */     this.dsi.md.put("lp", this.localPath = localPath);
/*     */   }
/*     */   
/*     */   public void setUidValidity(Long uidValidity) {
/*  69 */     this.dsi.md.put("uv", this.uidValidity = uidValidity);
/*     */   }
/*     */   
/*     */   protected void parseMetaData() throws ServiceException
/*     */   {
/*  74 */     this.localPath = this.dsi.md.get("lp", "");
/*  75 */     this.uidValidity = Long.valueOf(this.dsi.md.getLong("uv", -1L));
/*     */   }
/*     */   
/*     */   public ImapMessage getMessage(int itemId) throws ServiceException {
/*  79 */     return new ImapMessage(this.ds, itemId);
/*     */   }
/*     */   
/*     */   public ImapMessage getMessage(long uid) throws ServiceException {
/*  83 */     return new ImapMessage(this.ds, getItemId(), uid);
/*     */   }
/*     */   
/*     */   public ImapMessageCollection getMessages() throws ServiceException {
/*  87 */     Collection<DbDataSource.DataSourceItem> mappings = getMappingsAndFlags(this.ds, getItemId());
/*  88 */     ImapMessageCollection imc = new ImapMessageCollection();
/*     */     
/*  90 */     for (DbDataSource.DataSourceItem mapping : mappings)
/*  91 */       imc.add(new ImapMessage(this.ds, mapping));
/*  92 */     return imc;
/*     */   }
/*     */   
/*     */   public List<Integer> getNewMessageIds() throws ServiceException {
/*  96 */     Collection<DbDataSource.DataSourceItem> mappings = getMappingsAndFlags(this.ds, getItemId());
/*  97 */     Mailbox mbox = DataSourceManager.getInstance().getMailbox(this.ds);
/*  98 */     List<Integer> allIds = mbox.listItemIds(mbox.getOperationContext(), MailItem.Type.MESSAGE, getItemId());
/*  99 */     List<Integer> newIds = new ArrayList();
/*     */     
/*     */ 
/* 102 */     for (Integer id : allIds) {
/* 103 */       Iterator i$ = mappings.iterator(); for (;;) { if (!i$.hasNext()) break label136; DbDataSource.DataSourceItem mapping = (DbDataSource.DataSourceItem)i$.next();
/* 104 */         if (mapping.itemId == id.intValue()) {
/* 105 */           mappings.remove(mapping);
/* 106 */           break;
/*     */         }
/*     */       }
/* 109 */       newIds.add(id); }
/*     */     label136:
/* 111 */     return newIds;
/*     */   }
/*     */   
/*     */   public static ImapFolderCollection getFolders(DataSource ds) throws ServiceException {
/* 115 */     Collection<DbDataSource.DataSourceItem> mappings = getMappings(ds, ds.getFolderId());
/* 116 */     ImapFolderCollection ifc = new ImapFolderCollection();
/*     */     
/* 118 */     if (mappings.size() == 0) {
/* 119 */       Mailbox mbox = DataSourceManager.getInstance().getMailbox(ds);
/*     */       
/* 121 */       ZimbraLog.datasource.info("Upgrading IMAP data for %s", new Object[] { ds.getName() });
/* 122 */       DbDataSource.deleteAllMappings(ds);
/*     */       try {
/* 124 */         for (ImapFolder folderTracker : DbImapFolder.getImapFolders(mbox, ds))
/*     */         {
/* 126 */           folderTracker.add();
/* 127 */           for (ImapMessage msgTracker : DbImapMessage.getImapMessages(mbox, ds, folderTracker))
/*     */           {
/* 129 */             msgTracker.add();
/*     */           }
/*     */         }
/*     */       } catch (Exception e) {
/* 133 */         DbDataSource.deleteAllMappings(ds);
/* 134 */         throw ServiceException.FAILURE("IMAP data upgrade failed for " + ds.getName(), e);
/*     */       }
/*     */       
/* 137 */       mappings = getMappings(ds, ds.getFolderId());
/* 138 */       DbImapFolder.deleteImapData(mbox, ds.getId());
/*     */     }
/* 140 */     for (DbDataSource.DataSourceItem mapping : mappings)
/* 141 */       ifc.add(new ImapFolder(ds, mapping));
/* 142 */     return ifc;
/*     */   }
/*     */   
/*     */   public String toString()
/*     */   {
/* 147 */     return String.format("ImapFolder: { itemId=%d, dataSourceId=%s, localPath=%s, remotePath=%s, uidValidity=%d }", new Object[] { Integer.valueOf(this.dsi.itemId), this.ds.getId(), this.localPath, this.dsi.remoteId, this.uidValidity });
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/datasource/imap/ImapFolder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */